package com.customer.web;

import com.customer.pojo.User;
import com.customer.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author kxk
 * @version 1.0
 * @ClassName: CustomerController
 * @Description: TODO 用户控制器
 * @Date: 2021-05-11 01:31
 */
@Controller
public class CustomerController {
    @Autowired
    UserService userService;

    // 进入首页
    @RequestMapping("/")
    public String toindex() {
        System.out.println("进入首页");
        return "forward:customerindex.jsp";
    }

    // 进入登录界面
    @RequestMapping("/tologin")
    public String tologin() {
        System.out.println("进入登录界面");
        return "customerlogin";
    }

    // 登录
    @RequestMapping("/login")
    public String login(HttpSession session, String utel, String upass, Model model) {
        System.out.println("++++++++++++++++++++++++++++");
        User onuser = userService.findByPassAndTel(utel, upass);
        if (onuser != null) {
            System.out.println("登录成功");
            session.setAttribute("onuser", onuser);
            return "redirect:customerindex.jsp";
        } else {
            System.out.println("密码输入错误");
            model.addAttribute("mess1", "密码输入错误,请重新输入");
            model.addAttribute("mess2", utel);
            return "redirect:customerlogin.jsp";
        }
    }

    //注销
    @RequestMapping("/exit")
    public String exit(HttpSession session) {
        session.invalidate();//设置session过期
        return "redirect:customerindex.jsp";
    }

    // 进入注册界面
    @RequestMapping("/toregist")
    public String toregist() {
        System.out.println("进入注册页面");
        return "customerregist";
    }

    // 注册
    @RequestMapping("/regist")
    public String regist(User user, Model model) {
        System.out.println(user);
        boolean newcus = userService.addUser(user);
        if (newcus) {
            model.addAttribute("message", "注册成功");
            return "redirect:/tologin.action";
        } else {
            model.addAttribute("message", "注册失败，请重新注册");
            System.out.println("注册失败，请重新注册");
            return "redirect:/toregist.action";
        }
    }

    //注册验证用户是否存在
    @RequestMapping("/getcustomerbyutel")
    @ResponseBody
    public String getcustomerbyutel(String utel) {
        System.out.println("注册验证用户是否存在");
        User user = userService.findByTel(utel);
        if (user!=null) {
            System.out.println("存在");
            return "true";
        } else {
            System.out.println("不存在");
            return "false";
        }
    }

    //去补充信息
    @RequestMapping("/toinfo")
    public String toinfo(){
        System.out.println("进入添加信息页面");
        return "info";
    }
    //补充信息
    @RequestMapping("/info")
    public String info(User user,Model model){
        System.out.println(user);
        boolean newcus = userService.addUser(user);
        System.out.println(newcus);
        if (newcus) {
            model.addAttribute("message", "更新成功");
            System.out.println("更新成功");
            return "forward:customerindex.jsp";
        }
        model.addAttribute("message", "更新失败");
        System.out.println("更新失败");
        return "redirect:/toinfo";
    }

    //上传头像
    @RequestMapping("/adduphoto")
    public String adduphoto(User user, @RequestParam("file") MultipartFile file) {
        try {
            String originame=file.getOriginalFilename();
            String path="D:/dir/"+originame;
            System.out.println(path);
            File newflie=new File(path);
            //将file转化成newfile的字节输出流
            file.transferTo(newflie);
            user.setUphoto(originame);
            boolean b = userService.addUser(user);
            System.out.println(b);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return "forward:customerindex.jsp ";

    }

    //后台控制器
    @RequestMapping("/admin")
    public String admin(){
        return "admin";
    }



    //删除用户
    @RequestMapping("deleteuser")
    public String deleteuaer(int[] ids){
        boolean i = userService.deleteUserByuids(ids);
        System.out.println(i);
        return "redirect:/userlist.action";
    }

    //展示用户列表
    @RequestMapping("userlist")
    public String userlist(Model model){
     List<User> list= userService.findAll();
     model.addAttribute("list",list);
        return "userlist";
    }
    //用户详情控制器
    @RequestMapping("/findbyid")
    public String findbyid(int uid, Model model) {
        User user = userService.findByuid(uid);
        model.addAttribute("in", user);
        System.out.println(user);
        return "findinfo";

    }


    // 进入聊天界面
    @RequestMapping("/tochat")
    public String tochat(){
        return "customerchat";
    }
}
